package com.dotto.atos.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.dotto.atos.R;
import com.dotto.atos.models.Branch;
import com.dotto.atos.utils.TextUtil;

public class FilterSpinner extends Dialog {

	private EditText inptFilter;
	private Button btnClear;
	private Button btnOk;
	private ListView lvOptions;
	private List<Branch> branchList;
	private int selectedCityId;

	public FilterSpinner(Context context, List<Branch> branchList,
			android.view.View.OnClickListener listener) {
		super(context, R.style.FullHeightDialog);
		setCancelable(true);
		setContentView(R.layout.filter_list_view);
		this.branchList = branchList;

		inptFilter = (EditText) findViewById(R.id.inpt_filter);
		btnClear = (Button) findViewById(R.id.btn_clear);
		btnOk = (Button) findViewById(R.id.btn_ok);
		lvOptions = (ListView) findViewById(R.id.lv_options);

		lvOptions.setOnItemClickListener(onClickListItem);

		btnClear.setOnClickListener(clear);
		if (listener != null)
			btnOk.setOnClickListener(listener);
		else
			btnOk.setOnClickListener(clear);

		inptFilter.addTextChangedListener(textWatcher);
		updateListview();

		setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				inptFilter.setText("");
				updateListview();
			}
		});
	}

	private void updateListview() {
		List<Map<String, String>> showBranchList = new ArrayList<Map<String, String>>();
		if (branchList != null && branchList.size() > 0) {
			for (int i = 0; i < branchList.size(); i++) {
				Branch branch = branchList.get(i);
				if (checkBracnhMatch(branch)) {
					showBranchList.add(createBranch(branch));
				}
			}
		}

		SimpleAdapter simpleAdpt = new SimpleAdapter(this.getContext(),
				showBranchList, R.layout.brach_list_item,
				new String[] { "branch" }, new int[] { R.id.item_text });
		lvOptions.setAdapter(simpleAdpt);
		lvOptions.invalidate();
	}

	private boolean checkBracnhMatch(Branch branch) {
		if (branch != null) {
			String filter = inptFilter.getText().toString();
			if (TextUtil.isEmpty(filter))
				return true;
			String name = getBranchName(branch);
			if (name != null && name.trim().contains(filter.trim()))
				return true;
		}
		return false;
	}

	private HashMap<String, String> createBranch(Branch branch) {
		HashMap<String, String> branchObj = new HashMap<String, String>();
		branchObj.put("branch", getBranchName(branch));
		branchObj.put("id", branch.getId() + "");
		return branchObj;
	}

	private String getBranchName(Branch branch) {
		if (branch != null) {
			String id = branch.getId() + "";
			if (id.length() < 4) {
				for (int i = 0; i < 4 - id.length(); i++)
					id = "0" + id;
			}
			return id + " - " + branch.getName();
		}
		return "";
	}

	public OnItemClickListener onClickListItem = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {

			try {
				@SuppressWarnings("unchecked")
				Map<String, String> map = (Map<String, String>) arg0
						.getItemAtPosition(arg2);
				String id = "-1";
				if (map != null)
					id = map.get("id");
				if (TextUtil.isEmpty(id))
					id = "-1";
				selectedCityId = -1;
				try {
					selectedCityId = Integer.parseInt(id);
				} catch (Exception e) {
				}
			} catch (Exception e) {
			}
		}
	};

	public int getSelectedItempostion() {
		return selectedCityId;
	}

	private TextWatcher textWatcher = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
			updateListview();
		}
	};

	private android.view.View.OnClickListener clear = new android.view.View.OnClickListener() {

		@Override
		public void onClick(View v) {
			inptFilter.setText("");
			updateListview();
		}
	};

}
